package com.synjones.zeuscloudapp.service.authorize;

import com.alibaba.fastjson.JSON;
import com.synjones.core.util.StringUtil;
import com.synjones.zeus.model.ExReqBean;
import com.synjones.zeus.model.ExRespBean;
import com.synjones.zeus.model.authorize.AuthorizeReqBean;
import com.synjones.zeuscloudapp.bean.CodeDescPair;
import com.synjones.zeuscloudapp.eureka.ZeusAuthorizeService;
import com.synjones.zeuscloudapp.service.AbstractDataPackServiceImpl;
import com.synjones.zeuscloudapp.util.BeanReqUtil;
import com.synjones.zeuscloudapp.util.ReqBean;
import com.synjones.zeuscloudapp.util.StatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Think
 * Description:
 * @date created in 10:45 2018-05-22
 * Modified by:
 */
@Service
public class AuthorizeService extends AbstractDataPackServiceImpl<AuthorizeReqBean, AuthorizeReqBean, String, String> {

    private final static Logger logger = LoggerFactory.getLogger(AuthorizeService.class);

    @Resource
    private ZeusAuthorizeService zeusAuthorizeService;

    /**
     *  获取request中内容后格式化
     *  @param reqBean HttpServletRequest
     *  @author Think
     *  Description: 获取request中内容后格式化
     *  @date 11:16 2018-05-22
     *  @return  ExReqBean<E>
     */
    @Override
    public ExReqBean<AuthorizeReqBean> buildReqBean(ReqBean<AuthorizeReqBean> reqBean) {
        ExReqBean<AuthorizeReqBean> copyFrom = BeanReqUtil.copyFrom(reqBean);
        copyFrom.setData(reqBean.getData());
        return copyFrom;
    }

    /**
     *  去eureka服务找到对应服务获取数据后格式化
     *  @param reqBean HttpServletRequest
     *  @author Think
     *  Description: 去eureka服务找到对应服务获取数据后格式化
     *  @date 11:16 2018-05-22
     *  @return  ExReqBean<MealConnectTimeBean>
     */
    @Override
    public ExRespBean<String> eureka(ExReqBean<AuthorizeReqBean> reqBean) {
        return zeusAuthorizeService.authorizeDevice(reqBean);
    }

    /**
     *  HttpServletResponse resBean中内容后格式化
     *  @param resBean HttpServletResponse
     *  @author Think
     *  Description: HttpServletResponse resBean中内容后格式化
     *  @date 11:16 2018-05-22
     *  @return  ExReqBean<E>
     */
    @Override
    public String buildResBean(ExRespBean<String> resBean) {
        return resBean.getData();
    }

    @Override
    public AuthorizeReqBean buildData(String jsonData) {
        AuthorizeReqBean authorizeReqBean = JSON.parseObject(jsonData, AuthorizeReqBean.class);
        return authorizeReqBean;
    }

    @Override
    public CodeDescPair verifyData(ReqBean<AuthorizeReqBean> reqBean) {
        CodeDescPair code = null;
        if (reqBean == null) {
            logger.info("登陆reqBean参数为空");
            code = new CodeDescPair(StatusEnum.PARAMS_ERROR.getStatusCode(), StatusEnum.PARAMS_ERROR.getMessage());
        } else if (reqBean.getData() == null) {
            logger.info("登陆data为空");
            code = new CodeDescPair(StatusEnum.PARAMS_ERROR.getStatusCode(), StatusEnum.PARAMS_ERROR.getMessage());
        } else if (StringUtil.isNullOrEmpty(reqBean.getData().getUserName())) {
            logger.info("用户名为空");
            code = new CodeDescPair(StatusEnum.CHECK_USERNAME_ERROR.getStatusCode(), StatusEnum.CHECK_USERNAME_ERROR.getMessage());
        } else if (StringUtil.isNullOrEmpty(reqBean.getData().getPassWord())) {
            logger.info("密码为空");
            code = new CodeDescPair(StatusEnum.CHECK_PASSWORD_ERROR.getStatusCode(), StatusEnum.CHECK_PASSWORD_ERROR.getMessage());
        }else if (StringUtil.isNullOrEmpty(reqBean.getData().getTerminalType())) {
            logger.info("设备类型");
            code = new CodeDescPair(StatusEnum.CHECH_TERMINALTYPE_ERROR.getStatusCode(), StatusEnum.CHECH_TERMINALTYPE_ERROR.getMessage());
        }else if(StringUtil.isNullOrEmpty(reqBean.getImei())){
            logger.info("imel为空");
            code = new CodeDescPair(StatusEnum.CHECK_IMEL_ERROR.getStatusCode(), StatusEnum.CHECK_IMEL_ERROR.getMessage());
        }
        return code;
    }
}
